﻿using Microsoft.AspNetCore.Mvc;
using PlatformNetCore.Common;
using PlatformNetCore.Common.Helper;
using PlatformNetCore.Entity;
using PlatformNetCore.IService;
using PlatformNetCore.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace PlatformNetCore.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class PartnerController : BaseController
    {
        private readonly IPartnerService partnerService;
        public PartnerController(IPartnerService _partnerService)
        {
            partnerService = _partnerService;
        }

        #region 合作伙伴
        [RoleAuthorize("PartnerList")]
        public IActionResult PartnerList(string search = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<Partner, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    return Json(partnerService.GetPartnerPagedList(pageindex, pagesize, predicate));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("PartnerAdd")]
        public IActionResult PartnerAdd()
        {
            try
            {
                Partner model = new Partner();
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("PartnerAdd")]
        public IActionResult PartnerAdd(Partner model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(partnerService.AddPartner(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("PartnerDel")]
        public IActionResult PartnerDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(partnerService.UpdatePartnerState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("PartnerEdit")]
        public IActionResult PartnerEdit(string id)
        {
            try
            {
                ResultResponse<Partner> result = partnerService.GetPartner(id);
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("PartnerEdit")]
        public IActionResult PartnerEdit(Partner model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(partnerService.UpdatePartner(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

    }
}